home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 October / EnigmA AMIGA RUN 01 (1995)(G.R. Edizioni)(IT)[!][issue 1995-10][Aminet 7].iso / Aminet / mus / play / tracker_4_31.lzh / tracker / Arch / MM1 / ui.c < prev   
C/C++ Source or Header  |  1995-04-22  |  5KB  |  279 lines

  1. /* Arch/MM1/ui.c 
  2.     vi:ts=3 sw=3:
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <signal.h>
  7. #include <sgstat.h>
  8. #include "defs.h"
  9. #include "extern.h"
  10. #include "tags.h"
  11. #include "prefs.h"
  12.  
  13. LOCAL void nonblocking_io P((void));
  14. LOCAL void sane_tty P((void));
  15. LOCAL void (*INIT)P((void)) = nonblocking_io;
  16.  
  17. LOCAL int show;
  18. /* poor man's timer */
  19. LOCAL int current_pattern;
  20. LOCAL int count_pattern, count_song;
  21. #define SMALL_DELAY 25
  22.  
  23. LOCAL struct sgbuf sanity;
  24. LOCAL struct sgbuf *psanity = 0;
  25.  
  26. LOCAL int is_fg;
  27.  
  28. /* signal handler */
  29.  
  30. LOCAL void goodbye(sig)
  31. int sig;
  32. {
  33.    static char buffer[25];
  34.  
  35.    sprintf(buffer, "Signal %d", sig);
  36.    end_all(buffer);
  37. }
  38.  
  39. LOCAL void abort_this(sig)
  40. int sig;
  41. {
  42.    end_all("Abort");
  43. }
  44.  
  45.  
  46. int run_in_fg()
  47. {
  48.    int val;
  49.  
  50.    if (!isatty(fileno(stdin)) || !isatty(fileno(stdout)))
  51.       return FALSE;
  52.  
  53.    return TRUE;
  54. }
  55.  
  56. LOCAL void switch_mode()
  57. {
  58.    struct sgbuf zap;
  59.  
  60.    signal(SIGINT, goodbye);
  61.    signal(SIGQUIT, goodbye);
  62.    signal(SIGUSR1, abort_this);
  63.  
  64.    if (run_in_fg()) {
  65.       _gs_opt(fileno(stdin), &zap);
  66.  
  67.       zap.sg_pause = 0;    /* no pause at end of page */
  68.       zap.sg_backsp = 0;     /* nondestructive backspace */
  69.       zap.sg_delete = 0; /* no delete sequence */
  70.       zap.sg_echo = 0; /* don't echo chars */
  71.       zap.sg_dlnch = 0xff; /* ^X delete line character */
  72.       zap.sg_rlnch = 0xff; /* ^D reprint line character */
  73.       zap.sg_dulnch = 0xff; /* ^A duplicate line character */
  74.       zap.sg_psch = 0xff; /* ^W pause character */
  75.       zap.sg_eofch = 0xff; /* escape key in OS-9 */
  76.       zap.sg_kbich = 0xff; /* ^C interrupt character */
  77.       zap.sg_kbach = 0xff; /* ^E abort character */
  78.       /* Note ^S and ^Q are disabled! */
  79.       zap.sg_xon = 0xff;   /* ^Q XON */
  80.       zap.sg_xoff = 0xff;  /* ^S XOFF */
  81.  
  82.       _ss_opt(fileno(stdin), &zap);
  83.  
  84.       is_fg = TRUE;
  85.    } else
  86.       is_fg = FALSE;
  87. }
  88.  
  89. /* nonblocking_io():
  90.  * try to setup the keyboard to non blocking io
  91.  */
  92. LOCAL void nonblocking_io()
  93. {
  94.    show = get_pref_scalar(PREF_SHOW);
  95.  
  96.    if (!psanity) {
  97.       psanity = &sanity;
  98.       _gs_opt(fileno(stdin), psanity);
  99.    }
  100.    switch_mode();
  101.    at_end(sane_tty);
  102. }
  103.  
  104.  
  105. /* sane_tty():
  106.  * restores everything to a sane state before returning to shell */
  107. LOCAL void sane_tty()
  108. {
  109.    _ss_opt(fileno(stdin), psanity);
  110. }
  111.  
  112. LOCAL int may_getchar()
  113. {
  114.    char buffer;
  115.  
  116.    INIT_ONCE;
  117.  
  118.    if (run_in_fg() && !is_fg)
  119.       switch_mode();
  120.    if (run_in_fg() && (_gs_rdy(fileno(stdin)) > 0) ) {
  121.       read(fileno(stdin), &buffer, 1);
  122.       return buffer;
  123.    }
  124.    return EOF;
  125. }
  126.  
  127. LOCAL struct tag result[2];
  128.  
  129. struct tag *get_ui()
  130. {
  131.    result[0].type = TAG_END;
  132.    result[1].type = TAG_END;
  133.    count_pattern++;
  134.    count_song++;
  135.    switch(may_getchar()) {
  136.    case 'n':
  137.       result[0].type = UI_NEXT_SONG;
  138.       break;
  139.    case 'p':
  140.       if (count_song > SMALL_DELAY)
  141.          result[0].type = UI_RESTART;
  142.       else
  143.          result[0].type = UI_PREVIOUS_SONG;
  144.       count_song = 0;
  145.       break;
  146.    case 'x':
  147.    case 'e':
  148.    case 'q':
  149.       result[0].type = UI_QUIT;
  150.       break;
  151.    case 's':
  152.       result[0].type = UI_SET_BPM;
  153.       result[0].data.scalar = 50;
  154.       break;
  155.    case 'S':
  156.       result[0].type = UI_SET_BPM;
  157.       result[0].data.scalar = 60;
  158.       break;
  159.    case '>':
  160.       result[0].type = UI_JUMP_TO_PATTERN;
  161.       result[0].data.scalar = current_pattern + 1;
  162.       break;
  163.    case '<':
  164.       result[0].type = UI_JUMP_TO_PATTERN;
  165.       result[0].data.scalar = current_pattern;
  166.       if (count_pattern < SMALL_DELAY)
  167.          result[0].data.scalar--;
  168.       break;
  169.    case '?':
  170.       show = !show;
  171.       set_pref_scalar(PREF_SHOW, show);
  172.       if (show)
  173.          putchar('\n');
  174.       break;
  175.    default:
  176.       break;
  177.    }
  178.    return result;
  179. }
  180.  
  181. void notice(s)
  182. char *s;
  183. {
  184.    fprintf(stderr, "%s\n", s);
  185. }
  186.  
  187. void status(s)
  188. char *s;
  189. {
  190.    if (run_in_fg()) {
  191.       if (s)
  192.          puts(s);
  193.       else
  194.          putchar('\n');
  195.    }
  196. }
  197.  
  198. LOCAL char title[25];
  199.  
  200. void song_title(s)
  201. char *s;
  202. {
  203.    strncpy(title, s, 25);
  204.    if (run_in_fg() && !show)
  205.       puts(title);
  206.    count_song = 0;
  207. }
  208.  
  209. LOCAL char scroll_buffer[80];
  210.  
  211. GENERIC begin_info(title)
  212. char *title;
  213. {
  214.    if (run_in_fg())
  215.       return scroll_buffer;
  216.    else
  217.       return 0;
  218. }
  219.  
  220. void infos(handle, s)
  221. GENERIC handle;
  222. char *s;
  223. {
  224.    if (handle)
  225.       printf(s);
  226. }
  227.  
  228. void info(handle, line)
  229. GENERIC handle;
  230. char *line;
  231. {
  232.    if (handle)
  233.       puts(line);
  234. }
  235.  
  236. void end_info(handle)
  237. GENERIC handle;
  238. {
  239.    if (handle)
  240.       fflush(stdout);
  241. }
  242.  
  243. LOCAL char *last_result = 0;
  244.  
  245. char *new_scroll(void)
  246. {
  247.    if (run_in_fg()) {
  248.       last_result = scroll_buffer;
  249.       strcpy(scroll_buffer, "             |             |             |             ");
  250.    }
  251.    else
  252.       last_result = 0;
  253.    return last_result;
  254. }
  255.  
  256. void scroll()
  257. {
  258.    if (run_in_fg() && last_result) {
  259.       puts(scroll_buffer);
  260.       fflush(stdout);
  261.    }
  262. }
  263.  
  264. void display_pattern(current, total, real)
  265. int current, total, real;
  266.    {
  267.    if (run_in_fg())
  268.       {
  269.       if (show)
  270.          printf("\n%3d/%3d[%3d] %s\n", current, total, real, title);
  271.       else
  272.          printf("%3d/%3d\b\b\b\b\b\b\b", current, total);
  273.       fflush(stdout); 
  274.       }
  275.    current_pattern = current;
  276.    count_pattern = 0;
  277.    }
  278.  
  279.